﻿using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Unity;
using appCtx = EasyInvoice.ApplicationContext.AppContext;
using System.ComponentModel;
using System.IO;
using System.Drawing;

namespace EasyInvoice.WPF.ViewModel
{
    public class ClientViewModel : NotifyPropertyChanged, IDataErrorInfo
    {
        private Model.Client _client {get;set;}

        private Services.ClientsService _serviceClients; 
        
        public ClientViewModel() : this( new Model.Client()){}

        public ClientViewModel(Model.Client client)
        {
            if (client == null) client = new Model.Client();
            this._client = client;
            _serviceClients = appCtx.Instance.IoC.Resolve<Services.ClientsService>();
        }

        public int Id
        {
            get
            {
                return this._client.Id;
            }
            set
            {
                this._client.Id = value;
                this.OnPropertyChanged("Id");
            }
        }

        public string Name
        {
            get
            {
                return this._client.Name;
            }
            set
            {
                this._client.Name = value;
                this.OnPropertyChanged("Name");
            }
        }

        public string Address
        {
            get
            {
                return this._client.Address;
            }
            set
            {
                this._client.Address = value;
                this.OnPropertyChanged("Address");
            }
        }

        public string PostalCode
        {
            get
            {
                return this._client.PostalCode;
            }
            set
            {
                this._client.PostalCode = value;
                this.OnPropertyChanged("PostalCode");
            }
        }
        
        public string City
        {
            get
            {
                return this._client.City;
            }
            set
            {
                this._client.City = value;
                this.OnPropertyChanged("City");
            }
        }

        public string State
        {
            get
            {
                return this._client.State;
            }
            set
            {
                this._client.State = value;
                this.OnPropertyChanged("State");
            }
        }
        
        public string Country
        {
            get
            {
                return this._client.Country;
            }
            set
            {
                this._client.Country = value;
                this.OnPropertyChanged("Country");
            }
        }

        public string Phone
        {
            get
            {
                return this._client.Phone;
            }
            set
            {
                this._client.Phone = value;
                this.OnPropertyChanged("Phone");
            }
        }

        public string Email
        {
            get
            {
                return this._client.Email;
            }
            set
            {
                this._client.Email = value;
                this.OnPropertyChanged("Email");
            }
        }

        public bool IsMaster
        {
            get
            {
                return this._client.IsMaster;
            }
            set
            {
                this._client.IsMaster = value;
                this.OnPropertyChanged("IsMaster");
            }
        }

        public byte[] Signature 
        { 
            get 
            { 
                return this._client.Signature; 
            } 
            set 
            { 
                this._client.Signature = value;
                this.OnPropertyChanged("Signature");
            } 
        }


        internal void Delete()
        {
            this._serviceClients.Delete(this._client);
        }

        //for opening up the Add Client window
        private ICommand showSaveCommand;

        public ICommand ShowSaveCommand
        {
            get
            {
                if (showSaveCommand == null)
                {
                    showSaveCommand = new Common.CommandBase(
                        i => this.SaveClient(),
                        i => ((UserViewModel)Properties.Settings.Default.AuthenticatedUser) == null ? false :
                            this.Id == 0 ? ((UserViewModel)Properties.Settings.Default.AuthenticatedUser).AddClients :
                            ((UserViewModel)Properties.Settings.Default.AuthenticatedUser).EditClients);
                }
                return showSaveCommand;
            }
        }

        private void SaveClient()
        {

            //save client
            try
            {
                this._serviceClients.Save(this._client);
                Repository.Reset();
                //Common.VisualHelper.FindChild<Content.ClientList>(null,"")
               // (FindResource("clientsComboDataSource") as ObjectDataSource).Refresh();
            }
            catch (Exception ex)
            {
                ModernDialog.ShowMessage(ex.Message, "Save Client Failed", MessageBoxButton.OK);
            }

           
        }

        private ICommand showBrowseCommand;

        public ICommand ShowBrowseCommand
        {
            get
            {
                if (showBrowseCommand == null)
                {
                    showBrowseCommand = new Common.CommandBase(i => this.ShowBrowse(), i=> Id>0);
                }
                return showBrowseCommand;
            }
        }

        private void ShowBrowse()
        {
            var fileBrowser = new Common.BrowseFiles()
            {
                Title = "Browse for Images",
                Description = "select signature image file (*.png)",
                filterExtension = ".png"
            };
            if (fileBrowser.ShowDialog() == true)
            {
                System.Drawing.Image signatureImage = Image.FromFile(fileBrowser.SelectedPath);
                MemoryStream ms = new MemoryStream();
                signatureImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                Signature = ms.ToArray();
            }
            
            this.OnPropertyChanged("Signature");
            this.ShowSaveCommand.Execute(null);
        }

        private ICommand showImageCommand;

        public ICommand ShowImageCommand
        {
            get
            {
                if (showImageCommand == null)
                {
                    showImageCommand = new Common.CommandBase(i => this.ShowImage(), i => Signature != null );
                }
                return showImageCommand;
            }
        }

        private void ShowImage()
        {
            var signatureWindow = new ModernDialog()
                {
                    Content = new Content.ClientSignature()
                                  {
                                      DataContext = this,
                                  },
                    Title = this.Name,
                };

            
            signatureWindow.ShowDialog();

        }

        public string Error
        {
            get { return null; }
        }
        public string this[string columnName]
        {
            get
            {
                if (columnName == "Name")
                {
                    return string.IsNullOrEmpty(this.Name) ? "Required value" : null;
                }
                if (columnName == "Address")
                {
                    return string.IsNullOrEmpty(this.Address) ? "Required value" : null;
                }
                if (columnName == "PostalCode")
                {
                    return string.IsNullOrEmpty(this.PostalCode) ? "Required value" : null;
                }
                if (columnName == "City")
                {
                    return string.IsNullOrEmpty(this.City) ? "Required value" : null;
                }
                if (columnName == "State")
                {
                    return string.IsNullOrEmpty(this.State) ? "Required value" : null;
                }
                if (columnName == "Country")
                {
                    return string.IsNullOrEmpty(this.Country) ? "Required value" : null;
                }
                if (columnName == "Phone")
                {
                    return string.IsNullOrEmpty(this.Phone) ? "Required value" : null;
                }
                if (columnName == "Email")
                {
                    return string.IsNullOrEmpty(this.Email) ? "Required value" : null;
                }
                return null;
            }
        }
    }
}
